#include <iostream>
#include <vector>
#include <string>
#include <cassert>
#include <cstring>
#include <ctime>
#include <functional>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <memory>
#include <typeinfo>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/epoll.h>
#include <sys/eventfd.h>
#include <sys/timerfd.h>

#define INF 0
#define DBG 1
#define ERR 2
#define LOG_LEVEL DBG

#define LOG(level, format, ...)                                                                                        \
    do                                                                                                                 \
    {                                                                                                                  \
        if (level < LOG_LEVEL)                                                                                         \
            break;                                                                                                     \
        time_t t = time(NULL);                                                                                         \
        struct tm *ltm = localtime(&t);                                                                                \
        char tmp[32] = {0};                                                                                            \
        strftime(tmp, 31, "%H:%M:%S", ltm);                                                                            \
        fprintf(stdout, "[%p %s %s:%d] " format "\n", (void *)pthread_self(), tmp, __FILE__, __LINE__, ##__VA_ARGS__); \
    } while (0)

#define INF_LOG(format, ...) LOG(INF, format, ##__VA_ARGS__)
#define DBG_LOG(format, ...) LOG(DBG, format, ##__VA_ARGS__)
#define ERR_LOG(format, ...) LOG(ERR, format, ##__VA_ARGS__)

#define BUFFER_DEFAULT_SIZE 1024
class Buffer
{
private:
    std::vector<char> _buffer; // NOTE(zhangshuancheng)使用vector进行内存空间管理
    uint64_t _reader_idx;      // NOTE(zhangshuancheng)读偏移
    uint64_t _writer_idx;      // NOTE(zhangshuancheng)写偏移
public:
    Buffer() : _reader_idx(0), _writer_idx(0), _buffer(BUFFER_DEFAULT_SIZE) {}
    char *Begin() { return &*_buffer.begin(); }
    // NOTE(zhangshuancheng)获取当前写入起始地址, _buffer的空间起始地址，加上写偏移量
    char *WritePosition() { return Begin() + _writer_idx; }
    // NOTE(zhangshuancheng)获取当前读取起始地址
    char *ReadPosition() { return Begin() + _reader_idx; }
    // NOTE(zhangshuancheng)获取缓冲区末尾空闲空间大小--写偏移之后的空闲空间, 总体空间大小减去写偏移
    uint64_t TailIdleSize() { return _buffer.size() - _writer_idx; }
    // NOTE(zhangshuancheng)获取缓冲区起始空闲空间大小--读偏移之前的空闲空间
    uint64_t HeadIdleSize() { return _reader_idx; }
    // NOTE(zhangshuancheng)获取可读数据大小 = 写偏移 - 读偏移
    uint64_t ReadAbleSize() { return _writer_idx - _reader_idx; }
    // NOTE(zhangshuancheng)将读偏移向后移动
    void MoveReadOffset(uint64_t len)
    {
        if (len == 0)
            return;
        // NOTE(zhangshuancheng)向后移动的大小，必须小于可读数据大小
        assert(len <= ReadAbleSize());
        _reader_idx += len;
    }
    // NOTE(zhangshuancheng)将写偏移向后移动
    void MoveWriteOffset(uint64_t len)
    {
        // NOTE(zhangshuancheng)向后移动的大小，必须小于当前后边的空闲空间大小
        assert(len <= TailIdleSize());
        _writer_idx += len;
    }
    // NOTE(zhangshuancheng)确保可写空间足够（整体空闲空间够了就移动数据，否则就扩容）
    void EnsureWriteSpace(uint64_t len)
    {
        // NOTE(zhangshuancheng)如果末尾空闲空间大小足够，直接返回
        if (TailIdleSize() >= len)
        {
            return;
        }
        // NOTE(zhangshuancheng)末尾空闲空间不够，则判断加上起始位置的空闲空间大小是否足够, 够了就将数据移动到起始位置
        if (len <= TailIdleSize() + HeadIdleSize())
        {
            // NOTE(zhangshuancheng)将数据移动到起始位置
            uint64_t rsz = ReadAbleSize();                            // NOTE(zhangshuancheng)把当前数据大小先保存起来
            std::copy(ReadPosition(), ReadPosition() + rsz, Begin()); // NOTE(zhangshuancheng)把可读数据拷贝到起始位置
            _reader_idx = 0;                                          // NOTE(zhangshuancheng)将读偏移归0
            _writer_idx = rsz;                                        // NOTE(zhangshuancheng)将写位置置为可读数据大小， 因为当前的可读数据大小就是写偏移量
        }
        else
        {
            // NOTE(zhangshuancheng)总体空间不够，则需要扩容，不移动数据，直接给写偏移之后扩容足够空间即可
            DBG_LOG("RESIZE %ld", _writer_idx + len);
            _buffer.resize(_writer_idx + len);
        }
    }
    // NOTE(zhangshuancheng)写入数据
    void Write(const void *data, uint64_t len)
    {
        // NOTE(zhangshuancheng)1. 保证有足够空间，2. 拷贝数据进去
        if (len == 0)
            return;
        EnsureWriteSpace(len);
        const char *d = (const char *)data;
        std::copy(d, d + len, WritePosition());
    }
    void WriteAndPush(const void *data, uint64_t len)
    {
        Write(data, len);
        MoveWriteOffset(len);
    }
    void WriteString(const std::string &data)
    {
        return Write(data.c_str(), data.size());
    }
    void WriteStringAndPush(const std::string &data)
    {
        WriteString(data);
        MoveWriteOffset(data.size());
    }
    void WriteBuffer(Buffer &data)
    {
        return Write(data.ReadPosition(), data.ReadAbleSize());
    }
    void WriteBufferAndPush(Buffer &data)
    {
        WriteBuffer(data);
        MoveWriteOffset(data.ReadAbleSize());
    }
    // NOTE(zhangshuancheng)读取数据
    void Read(void *buf, uint64_t len)
    {
        // NOTE(zhangshuancheng)要求要获取的数据大小必须小于可读数据大小
        assert(len <= ReadAbleSize());
        std::copy(ReadPosition(), ReadPosition() + len, (char *)buf);
    }
    void ReadAndPop(void *buf, uint64_t len)
    {
        Read(buf, len);
        MoveReadOffset(len);
    }
    std::string ReadAsString(uint64_t len)
    {
        // NOTE(zhangshuancheng)要求要获取的数据大小必须小于可读数据大小
        assert(len <= ReadAbleSize());
        std::string str;
        str.resize(len);
        Read(&str[0], len);
        return str;
    }
    std::string ReadAsStringAndPop(uint64_t len)
    {
        assert(len <= ReadAbleSize());
        std::string str = ReadAsString(len);
        MoveReadOffset(len);
        return str;
    }
    char *FindCRLF()
    {
        char *res = (char *)memchr(ReadPosition(), '\n', ReadAbleSize());
        return res;
    }
    /*通常获取一行数据，这种情况针对是*/
    std::string GetLine()
    {
        char *pos = FindCRLF();
        if (pos == NULL)
        {
            return "";
        }
        // NOTE(zhangshuancheng) +1是为了把换行字符也取出来。
        return ReadAsString(pos - ReadPosition() + 1);
    }
    std::string GetLineAndPop()
    {
        std::string str = GetLine();
        MoveReadOffset(str.size());
        return str;
    }
    // NOTE(zhangshuancheng)清空缓冲区
    void Clear()
    {
        // NOTE(zhangshuancheng)只需要将偏移量归0即可
        _reader_idx = 0;
        _writer_idx = 0;
    }
};

#define MAX_LISTEN 1024
class Socket
{
private:
    int _sockfd;

public:
    Socket() : _sockfd(-1) {}
    Socket(int fd) : _sockfd(fd) {}
    ~Socket() { Close(); }
    int Fd() { return _sockfd; }
    // NOTE(zhangshuancheng)创建套接字
    bool Create()
    {
        // NOTE(zhangshuancheng) int socket(int domain, int type, int protocol)
        _sockfd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (_sockfd < 0)
        {
            ERR_LOG("CREATE SOCKET FAILED!!");
            return false;
        }
        return true;
    }
    // NOTE(zhangshuancheng)绑定地址信息
    bool Bind(const std::string &ip, uint16_t port)
    {
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        socklen_t len = sizeof(struct sockaddr_in);
        // NOTE(zhangshuancheng) int bind(int sockfd, struct sockaddr*addr, socklen_t len);
        int ret = bind(_sockfd, (struct sockaddr *)&addr, len);
        if (ret < 0)
        {
            ERR_LOG("BIND ADDRESS FAILED!");
            return false;
        }
        return true;
    }
    // NOTE(zhangshuancheng)开始监听
    bool Listen(int backlog = MAX_LISTEN)
    {
        // NOTE(zhangshuancheng) int listen(int backlog)
        int ret = listen(_sockfd, backlog);
        if (ret < 0)
        {
            ERR_LOG("SOCKET LISTEN FAILED!");
            return false;
        }
        return true;
    }
    // NOTE(zhangshuancheng)向服务器发起连接
    bool Connect(const std::string &ip, uint16_t port)
    {
        struct sockaddr_in addr;
        addr.sin_family = AF_INET;
        addr.sin_port = htons(port);
        addr.sin_addr.s_addr = inet_addr(ip.c_str());
        socklen_t len = sizeof(struct sockaddr_in);
        // NOTE(zhangshuancheng) int connect(int sockfd, struct sockaddr*addr, socklen_t len);
        int ret = connect(_sockfd, (struct sockaddr *)&addr, len);
        if (ret < 0)
        {
            ERR_LOG("CONNECT SERVER FAILED!");
            return false;
        }
        return true;
    }
    // NOTE(zhangshuancheng)获取新连接
    int Accept()
    {
        // NOTE(zhangshuancheng) int accept(int sockfd, struct sockaddr *addr, socklen_t *len);
        int newfd = accept(_sockfd, NULL, NULL);
        if (newfd < 0)
        {
            ERR_LOG("SOCKET ACCEPT FAILED!");
            return -1;
        }
        return newfd;
    }
    // NOTE(zhangshuancheng)接收数据
    ssize_t Recv(void *buf, size_t len, int flag = 0)
    {
        // NOTE(zhangshuancheng) ssize_t recv(int sockfd, void *buf, size_t len, int flag);
        ssize_t ret = recv(_sockfd, buf, len, flag);
        if (ret <= 0)
        {
            // NOTE(zhangshuancheng)EAGAIN 当前socket的接收缓冲区中没有数据了，在非阻塞的情况下才会有这个错误
            // NOTE(zhangshuancheng)EINTR  表示当前socket的阻塞等待，被信号打断了，
            if (errno == EAGAIN || errno == EINTR)
            {
                return 0; // NOTE(zhangshuancheng)表示这次接收没有接收到数据
            }
            ERR_LOG("SOCKET RECV FAILED!!");
            return -1;
        }
        return ret; // NOTE(zhangshuancheng)实际接收的数据长度
    }
    ssize_t NonBlockRecv(void *buf, size_t len)
    {
        return Recv(buf, len, MSG_DONTWAIT); // NOTE(zhangshuancheng) MSG_DONTWAIT 表示当前接收为非阻塞。
    }
    // NOTE(zhangshuancheng)发送数据
    ssize_t Send(const void *buf, size_t len, int flag = 0)
    {
        // NOTE(zhangshuancheng) ssize_t send(int sockfd, void *data, size_t len, int flag);
        ssize_t ret = send(_sockfd, buf, len, flag);
        if (ret < 0)
        {
            if (errno == EAGAIN || errno == EINTR)
            {
                return 0;
            }
            ERR_LOG("SOCKET SEND FAILED!!");
            return -1;
        }
        return ret; // NOTE(zhangshuancheng)实际发送的数据长度
    }
    ssize_t NonBlockSend(void *buf, size_t len)
    {
        if (len == 0)
            return 0;
        return Send(buf, len, MSG_DONTWAIT); // NOTE(zhangshuancheng) MSG_DONTWAIT 表示当前发送为非阻塞。
    }
    // NOTE(zhangshuancheng)关闭套接字
    void Close()
    {
        if (_sockfd != -1)
        {
            close(_sockfd);
            _sockfd = -1;
        }
    }
    // NOTE(zhangshuancheng)创建一个服务端连接
    bool CreateServer(uint16_t port, const std::string &ip = "0.0.0.0", bool block_flag = false)
    {
        // NOTE(zhangshuancheng)1. 创建套接字，2. 绑定地址，3. 开始监听，4. 设置非阻塞， 5. 启动地址重用
        if (Create() == false)
            return false;
        if (block_flag)
            NonBlock();
        if (Bind(ip, port) == false)
            return false;
        if (Listen() == false)
            return false;
        ReuseAddress();
        return true;
    }
    // NOTE(zhangshuancheng)创建一个客户端连接
    bool CreateClient(uint16_t port, const std::string &ip)
    {
        // NOTE(zhangshuancheng)1. 创建套接字，2.指向连接服务器
        if (Create() == false)
            return false;
        if (Connect(ip, port) == false)
            return false;
        return true;
    }
    // NOTE(zhangshuancheng)设置套接字选项---开启地址端口重用
    void ReuseAddress()
    {
        // NOTE(zhangshuancheng) int setsockopt(int fd, int leve, int optname, void *val, int vallen)
        int val = 1;
        setsockopt(_sockfd, SOL_SOCKET, SO_REUSEADDR, (void *)&val, sizeof(int));
        val = 1;
        setsockopt(_sockfd, SOL_SOCKET, SO_REUSEPORT, (void *)&val, sizeof(int));
    }
    // NOTE(zhangshuancheng)设置套接字阻塞属性-- 设置为非阻塞
    void NonBlock()
    {
        // NOTE(zhangshuancheng)int fcntl(int fd, int cmd, ... /* arg */ );
        int flag = fcntl(_sockfd, F_GETFL, 0);
        fcntl(_sockfd, F_SETFL, flag | O_NONBLOCK);
    }
};

class Poller;
class Channel
{
private:
    int _fd;
    Poller *_poller;
    uint32_t _events;  // NOTE(zhangshuancheng) 当前需要监控的事件
    uint32_t _revents; // NOTE(zhangshuancheng) 当前连接触发的事件
    using EventCallback = std::function<void()>;
    EventCallback _read_callback;  // NOTE(zhangshuancheng)可读事件被触发的回调函数
    EventCallback _write_callback; // NOTE(zhangshuancheng)可写事件被触发的回调函数
    EventCallback _error_callback; // NOTE(zhangshuancheng)错误事件被触发的回调函数
    EventCallback _close_callback; // NOTE(zhangshuancheng)连接断开事件被触发的回调函数
    EventCallback _event_callback; // NOTE(zhangshuancheng)任意事件被触发的回调函数
public:
    Channel(Poller *poller, int fd) : _fd(fd), _events(0), _revents(0), _poller(poller) {}
    int Fd() { return _fd; }
    uint32_t Events() { return _events; }                   // NOTE(zhangshuancheng)获取想要监控的事件
    void SetREvents(uint32_t events) { _revents = events; } // NOTE(zhangshuancheng)设置实际就绪的事件
    void SetReadCallback(const EventCallback &cb) { _read_callback = cb; }
    void SetWriteCallback(const EventCallback &cb) { _write_callback = cb; }
    void SetErrorCallback(const EventCallback &cb) { _error_callback = cb; }
    void SetCloseCallback(const EventCallback &cb) { _close_callback = cb; }
    void SetEventCallback(const EventCallback &cb) { _event_callback = cb; }
    // NOTE(zhangshuancheng)当前是否监控了可读
    bool ReadAble() { return (_events & EPOLLIN); }
    // NOTE(zhangshuancheng)当前是否监控了可写
    bool WriteAble() { return (_events & EPOLLOUT); }
    // NOTE(zhangshuancheng)启动读事件监控
    void EnableRead()
    {
        _events |= EPOLLIN;
        Update();
    }
    // NOTE(zhangshuancheng)启动写事件监控
    void EnableWrite()
    {
        _events |= EPOLLOUT;
        Update();
    }
    // NOTE(zhangshuancheng)关闭读事件监控
    void DisableRead()
    {
        _events &= ~EPOLLIN;
        Update();
    }
    // NOTE(zhangshuancheng)关闭写事件监控
    void DisableWrite()
    {
        _events &= ~EPOLLOUT;
        Update();
    }
    // NOTE(zhangshuancheng)关闭所有事件监控
    void DisableAll()
    {
        _events = 0;
        Update();
    }
    // NOTE(zhangshuancheng)移除监控
    void Remove();
    void Update();
    // NOTE(zhangshuancheng)事件处理，一旦连接触发了事件，就调用这个函数，自己触发了什么事件如何处理自己决定
    void HandleEvent()
    {
            
        // 下面的是标准的写法，但是用我的 server.cc 和 client.cc 测试的时候是有问题的。还是用自己的那种 if, else if, else if ... 的写法了
        if ((_revents & EPOLLIN) || (_revents & EPOLLRDHUP) || (_revents & EPOLLPRI))
        {
            /*不管任何事件，都调用的回调函数*/
            if (_read_callback)
                _read_callback();
        }

        if (_revents & EPOLLOUT) /*有可能会释放连接的操作事件，一次只处理一个*/
        {
            if (_write_callback)
                _write_callback();
        }
        else if (_revents & EPOLLERR)
        {
            if (_error_callback)
                _error_callback(); // NOTE(zhangshuancheng)一旦出错，就会释放连接，因此要放到前边调用任意回调
        }
        else if (_revents & EPOLLHUP)
        {
            if (_close_callback)
                _close_callback();
        }

        if (_event_callback)
            _event_callback();
    }
};
#define MAX_EPOLLEVENTS 1024
class Poller
{
private:
    int _epfd;
    struct epoll_event _evs[MAX_EPOLLEVENTS];
    std::unordered_map<int, Channel *> _channels;

private:
    // NOTE(zhangshuancheng)对epoll的直接操作
    void Update(Channel *channel, int op)
    {
        // NOTE(zhangshuancheng) int epoll_ctl(int epfd, int op,  int fd,  struct epoll_event *ev);
        int fd = channel->Fd();
        struct epoll_event ev;
        ev.data.fd = fd;
        ev.events = channel->Events();
        int ret = epoll_ctl(_epfd, op, fd, &ev);
        if (ret < 0)
        {
            ERR_LOG("EPOLLCTL FAILED!");
        }
        return;
    }
    // NOTE(zhangshuancheng)判断一个Channel是否已经添加了事件监控
    bool HasChannel(Channel *channel)
    {
        auto it = _channels.find(channel->Fd());
        if (it == _channels.end())
        {
            return false;
        }
        return true;
    }

public:
    Poller()
    {
        _epfd = epoll_create(MAX_EPOLLEVENTS);
        if (_epfd < 0)
        {
            ERR_LOG("EPOLL CREATE FAILED!!");
            abort(); // NOTE(zhangshuancheng)退出程序
        }
    }
    // NOTE(zhangshuancheng)添加或修改监控事件
    void UpdateEvent(Channel *channel)
    {
        bool ret = HasChannel(channel);
        if (ret == false)
        {
            // NOTE(zhangshuancheng)不存在则添加
            _channels.insert(std::make_pair(channel->Fd(), channel));
            return Update(channel, EPOLL_CTL_ADD);
        }
        return Update(channel, EPOLL_CTL_MOD);
    }
    // NOTE(zhangshuancheng)移除监控
    void RemoveEvent(Channel *channel)
    {
        auto it = _channels.find(channel->Fd());
        if (it != _channels.end())
        {
            _channels.erase(it);
        }
        Update(channel, EPOLL_CTL_DEL);
    }
    // NOTE(zhangshuancheng)开始监控，返回活跃连接
    void Poll(std::vector<Channel *> *active)
    {
        // NOTE(zhangshuancheng) int epoll_wait(int epfd, struct epoll_event *evs, int maxevents, int timeout)
        int nfds = epoll_wait(_epfd, _evs, MAX_EPOLLEVENTS, -1);
        if (nfds < 0)
        {
            if (errno == EINTR)
            {
                return;
            }
            ERR_LOG("EPOLL WAIT ERROR:%s\n", strerror(errno));
            abort(); // NOTE(zhangshuancheng)退出程序
        }
        for (int i = 0; i < nfds; i++)
        {
            auto it = _channels.find(_evs[i].data.fd);
            assert(it != _channels.end());
            it->second->SetREvents(_evs[i].events); // NOTE(zhangshuancheng)设置实际就绪的事件
            active->push_back(it->second);
        }
        return;
    }
};

void Channel::Remove() { return _poller->RemoveEvent(this); }
void Channel::Update() { return _poller->UpdateEvent(this); }
